﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.Bliss.Services;

public partial class Controls_GetWallPosts : UserControlBase
{
    public int ProfileUserID
    {
        get
        {
            int profileUserID = 0;
            if (Request.QueryString["Id"] == null)
            {
                profileUserID = UserID.Value;
            }
            else
            {
                profileUserID = int.Parse(Request.QueryString["Id"].ToString());
            }
            return profileUserID;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (UserID.HasValue)
            {
                if (UserID.Value == ProfileUserID)
                {
                    pnlPostSection.Visible = true;
                }
                else
                {
                    pnlPostSection.Visible = false;
                }
            }
            BindGrid();
        }
    }
    protected void ButtonPostScrap_Click(object sender, EventArgs e)
    {
        if (UserID.HasValue)
        {
            IPostService service = new PostService();
            service.Post(UserID.Value, null, TextBoxScrap.Text);
            TextBoxScrap.Text = "";
            BindGrid();
        }
        else
        {
            Response.Redirect("Login.aspx");
        }
    }
    private void BindGrid()
    {
        IPostService service = new PostService();
        var res = service.GetPosts(CompanyID.Value);
        List<com.Bliss.Post> lstPost = res.DataCollection.ToList();
        lstPost.ForEach(p => p.Comments = p.Comments.OrderBy(q => q.SendDate).ToList());
        lstPost = lstPost.OrderByDescending(p => p.SendDate).ToList();
        GridViewWallPosts.PageIndex = GridViewWallPosts.PageIndex;
        GridViewWallPosts.DataSource = lstPost;
        GridViewWallPosts.DataBind();
    }
    private void BindGridToChild(GridView childGridView, int ParentID)
    {
        IPostService service = new PostService();
        var res = service.GetChildPosts(ParentID);
        List<com.Bliss.Post> lstPost = res.DataCollection.ToList();
        lstPost = lstPost.OrderBy(p => p.SendDate).ToList();
        childGridView.PageIndex = childGridView.PageIndex;
        childGridView.DataSource = lstPost;
        childGridView.DataBind();
    }

    protected void rptUserDetails_ItemCommand(object source, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("DeletePost"))
        {
            int postID = Convert.ToInt32(e.CommandArgument);
            IPostService service = new PostService();
            service.DeletePost(postID);
            GridView grdChild = ((ImageButton)e.CommandSource).Parent.Parent.Parent.Parent.Parent.Parent as GridView;
            GridViewRow gvRowParent = ((ImageButton)e.CommandSource).Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent as GridViewRow;
            HiddenField hdnID = gvRowParent.FindControl("hdnID") as HiddenField;
            BindGridToChild(grdChild, int.Parse(hdnID.Value));
        }
    }
    public void rptUserDetails_ItemDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow gvr = e.Row;
        ImageButton imgDeletePost = gvr.FindControl("imgDeletePost") as ImageButton;
        if (imgDeletePost != null)
        {
            imgDeletePost.Attributes.Add("onclick", "javascript:return confirm('Are you sure that you want to delete this Reply ?')");
            com.Bliss.Post post = (com.Bliss.Post)gvr.DataItem;

            if (post.PostedBy.ID == UserID.Value || post.Parent.PostedBy.ID == UserID.Value)
            {
                imgDeletePost.Visible = true;
            }
            else
            {
                imgDeletePost.Visible = false;
            }
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            System.Web.UI.HtmlControls.HtmlTableRow itemRow = gvr.FindControl("itemRow") as System.Web.UI.HtmlControls.HtmlTableRow;
            //itemRow.Attributes["onmouseover"] = "this.style.backgroundColor='Pink'";
            //itemRow.Attributes["onmouseover"] = "this.style.backgroundColor='#F5F5F5'";
            itemRow.Attributes["onmouseover"] = "this.style.backgroundColor='#F0F3F4'";
            itemRow.Attributes["onmouseout"] = "this.style.backgroundColor='Transparent'";
        }
    }
    protected void rptUserDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView grdChild = (GridView)sender;
        GridViewRow gvRowParent = ((GridView)sender).Parent.Parent as GridViewRow;
        HiddenField hdnID = gvRowParent.FindControl("hdnID") as HiddenField;
        grdChild.PageIndex = e.NewPageIndex;
        BindGridToChild(grdChild, int.Parse(hdnID.Value));
    }
    protected void GridViewWallPosts_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        GridViewRow gvr = null;
        HiddenField hdnID = null;
        int ParentID = 0;
        if (e.CommandName == "Reply")
        {
            gvr = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
            hdnID = gvr.FindControl("hdnID") as HiddenField;
            if (hdnID != null)
            {
                ParentID = Convert.ToInt32(hdnID.Value);
                TextBox txtReply = gvr.FindControl("txtReply") as TextBox;
                if (txtReply != null)
                {
                    string message = txtReply.Text;
                    IPostService service = new PostService();
                    service.Post(UserID.Value, ParentID, message);

                    GridView rptUserDetails = gvr.FindControl("rptUserDetails") as GridView;
                    if (rptUserDetails != null)
                    {
                        rptUserDetails.PageIndex = rptUserDetails.PageCount;
                        BindGridToChild(rptUserDetails, ParentID);
                    }
                    txtReply.Text = "";
                }
            }
        }
        else if (e.CommandName == "DeleteMainPost")
        {
            gvr = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
            hdnID = gvr.FindControl("hdnID") as HiddenField;
            if (hdnID != null)
            {
                ParentID = Convert.ToInt32(hdnID.Value);
                IPostService service = new PostService();
                service.DeletePost(ParentID);
                BindGrid();
                //Response.Redirect("UserDetails.aspx?Id=" + ProfileUserID);
            }
        }

    }
    protected void GridViewWallPosts_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow gvr = e.Row;
        ImageButton imgDeleteMainPost = gvr.FindControl("imgDeleteMainPost") as ImageButton;
        if (imgDeleteMainPost != null)
        {
            imgDeleteMainPost.Attributes.Add("onclick", "javascript:return confirm('Are you sure that you want to delete this Post ?')");
            com.Bliss.Post post = (com.Bliss.Post)gvr.DataItem;
            if (post.PostedBy.ID == UserID.Value)
            {
                imgDeleteMainPost.Visible = true;
            }
            else
            {
                imgDeleteMainPost.Visible = false;
            }
        }

        GridView rptUserDetails = gvr.FindControl("rptUserDetails") as GridView;
        if (rptUserDetails != null)
        {
            com.Bliss.Post post = e.Row.DataItem as com.Bliss.Post;
            rptUserDetails.DataSource = post.Comments;
            rptUserDetails.DataBind();
        }

        RequiredFieldValidator reqReply = gvr.FindControl("reqReply") as RequiredFieldValidator;
        Button btnReply = gvr.FindControl("btnReply") as Button;
        if (btnReply != null)
        {
            TextBox txtReply = gvr.FindControl("txtReply") as TextBox;
            string validationGroup = Guid.NewGuid().ToString();
            btnReply.ValidationGroup = validationGroup;
            reqReply.ValidationGroup = validationGroup;
        }
    }
    protected void GridViewWallPosts_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridViewWallPosts.PageIndex = e.NewPageIndex;
        BindGrid();
    }

}